package entity.props;

import com.hzheima.util.ImageUtil;
import constant.BallConstant;
import constant.PropsConstant;
import constant.UiConstant;
import entity.Ball;
import entity.FlyingObject;

import java.util.ArrayList;
import java.util.Random;

/**
 * @Author:gl
 * @Date: 2025/09/09 19:13
 * @Description:炸弹类
 */
public class Bomb extends FlyingObject {
    private int xSpeed; //水平方向的速度
    private int ySpeed;//竖直方向的速度
    private int startLoc;//初始位置
    private boolean startDown;
    private Random random = new Random();
    public static ArrayList<Bomb> bombs = new ArrayList<>();//buff的集合
    private static int count = 15;

    public Bomb() {
        image = ImageUtil.getImg("zd.png");
        width = image.getWidth()/5;
        height = image.getHeight()/5;
        startLoc = random.nextInt(2);
        //设置x,y
        if (startLoc == BallConstant.LEFT) {
            //随机生成在左边
            x = -width;
            //设置速度
            xSpeed = 2;

        } else if (startLoc == BallConstant.RIGHT) {
            //随机生成在右边
            x = UiConstant.APP_WIDTH;
            //设置速度
            xSpeed = -2;
        }
        y = -height;
        ySpeed = 2;
    }

    @Override
    protected Ball clone() throws CloneNotSupportedException {
        try {
            return (Ball) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new AssertionError(); // 不会发生
        }
    }

    public void move() {
        //水平移动
        this.x += xSpeed;
        //如果移动到屏幕一半左右就能有向下的速度
        if (startLoc == BallConstant.LEFT) {
            //说明是从左边出来的
            if (x >= 250) {
                //可以向下移动了
                startDown = true;
            }
        } else if (startLoc == BallConstant.RIGHT) {
            //说明是从右边出来的
            if (x <= 250) {
                //可以向下移动了
                startDown = true;
            }
        }
        if (startDown) {
            this.y += ySpeed;
        }
    }

    //创建炸弹
    public static void createBomb() {
        if (bombs.size() < PropsConstant.BOMB_MAXNUM) {
            count++;
            if (count % 40 == 0) {
                Bomb bulletBuff = new Bomb();
                bombs.add(bulletBuff);
            }
        }
    }
    //炸弹移动
    public static void bombMove() {
        for (int i = 0; i < bombs.size(); i++) {
            Bomb buff = bombs.get(i);
            buff.move();
        }
    }

    //炸弹撞墙反弹
    public void rebound() {
        if (xSpeed > 0 && x > UiConstant.APP_WIDTH - width) {
            xSpeed = -xSpeed;
        }
        if (xSpeed < 0 && x < 0) {
            xSpeed = -xSpeed;
        }
        if (ySpeed > 0 && y > UiConstant.APP_HEIGHT - height) {
            ySpeed = -ySpeed;
        }
        if (ySpeed < 0 && y < 0) {
            ySpeed = -ySpeed;
        }
    }

    @Override
    public int getxSpeed() {
        return xSpeed;
    }

    @Override
    public void setxSpeed(int xSpeed) {
        this.xSpeed = xSpeed;
    }

    @Override
    public int getySpeed() {
        return ySpeed;
    }

    @Override
    public void setySpeed(int ySpeed) {
        this.ySpeed = ySpeed;
    }

}
